package persistency;

/**
 * CodeRepository.java
 * Purpose: A generic storage facility for model objects.
 * Methods added here should be applicable to all model objects.
 * 
 * @author Hans Brems
 * @version 0.1 14/04/2012
 */

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import model.CodeType;

public class CodeTypeRepository implements Repository<CodeType> {
	/** Inserts a new entity into the database */
	@Override
	public void insert(CodeType entity) throws PersistencyException {
		EntityManager em = EntityManagerHelper.getEntityManager();
		EntityTransaction tx = em.getTransaction();

		try {
			tx.begin();
			em.persist(entity);
			tx.commit();
		}
		catch (Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}
			throw new PersistencyException("Oops!", e);
		}
		finally {
			if (em != null && em.isOpen()) {
				em.close();
			}
		}
	}

	/** Updates an existing entity */
	@Override
	public void update(CodeType entity) throws PersistencyException {
		EntityManager em = EntityManagerHelper.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		
		try {
			tx.begin();
			em.merge(entity);
			tx.commit();
		}
		catch (Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}
			throw new PersistencyException("Oops!", e);
		}
		finally {
			if (em != null && em.isOpen()) {
				em.close();
			}
		}
	}

	/** Deletes an existing entity */
	@Override
	public void delete(CodeType entity) throws PersistencyException {
		throw new PersistencyException("Please set entity inactive and update");
	}

	/** Retrieves all entities from the database */	
	@Override
	public List<CodeType> getAll() throws PersistencyException {
		EntityManager em = EntityManagerHelper.getEntityManager();
		List<CodeType> codeTypes;

		try {
			// Create a list of service calls
			codeTypes = new ArrayList<CodeType>();
			
			// Create a query to retrieve all service calls
			Query codeTypeQuery = em.createQuery("select ct from CodeType ct");
			
			// Fill the list (convert from raw objects to ServiceCall objects)
			for (Object object : codeTypeQuery.getResultList()) {
				CodeType codeType = (CodeType)object;
				codeTypes.add(codeType);
			}
			
			return codeTypes;
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistencyException("Could not read code types", e);
		}
		finally {
			em.close();
		}
	}

	/** Retrieves an entity by id */ 
	@Override
	public CodeType getById(Object id) throws PersistencyException {
		EntityManager em = EntityManagerHelper.getEntityManager();
		
		try {
			return em.find(CodeType.class, id);
		}
		catch (Exception e) {
			throw new PersistencyException("Could not find record", e);
		}
		finally {
			em.close();
		}
	}

	/** Returns a count of records in the database */
	@Override
	public int getCount() throws PersistencyException {
		EntityManager em = EntityManagerHelper.getEntityManager();

		try {
			return Integer.parseInt(em.createQuery("Select count(ct.codetype_id) from CodeType ct")
					.getSingleResult()
					.toString()); 
		}
		catch(Exception ex) {
			throw new PersistencyException("Could not count code types");
		}
		finally {
			em.close();
		}
	}
}